﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DotNet.Common.Kit
{
    /// <summary>
    /// Log Message Type enum
    /// </summary>
    public enum MessageType
    {
        /// <summary>
        /// 写异常信息
        /// </summary>
        Exception,
        /// <summary>
        /// 调试信息
        /// </summary>
        Debug,
        /// <summary>
        /// 错误信息。
        /// </summary>
        Error,
        /// <summary>
        /// success type
        /// </summary>
        Success
    }
    public enum DirType
    {
        //Winform项目本地日志
        Local,
        //Web项目的网络日志
        Net
    }

    public class LogerHelper
    {
        string _logDirectory;
        /// <summary>
        /// 写日志的位置和类型，位置可以分为网络(Web项目)和本地(Winform项目)
        /// 日志类型有debug,Exception等。
        /// </summary>
        /// <param name="dir">Net.Local，网络或者本地日志</param>
        /// <param name="msgType"></param>
        public LogerHelper(DirType dir,MessageType msgType)
        {
            if (dir == DirType.Local)
                _logDirectory = System.Windows.Forms.Application.StartupPath+"\\Error";
            if (dir == DirType.Net)
                _logDirectory = System.Web.HttpContext.Current.Server.MapPath("Error");
        }

        private string GetFileName()
        {
            DateTime now = DateTime.Now;
            string fileName = now.ToString("yyyyMMddHH");
            return fileName;
        }

        /// <summary>
        /// 一天一个日志
        /// 生成方式：目录：Error/年/月/日/文件名.log
        /// </summary>
        /// <param name="message"></param>
        public void LogToFile(string message)
        {
            try
            {
                DirectoryInfo dir=new DirectoryInfo(_logDirectory);
                string logFileName = _logDirectory + "\\" + GetFileName()+".log";
                if (!dir.Exists) //不存Error文件夹则创建
                {
                    dir.Create();
                    //File.Create(logFileName);
                }
                //else
                //{
                //    if (!File.Exists(logFileName))
                //    {
                //        File.Create(logFileName);
                //    }
                //}
                using (StreamWriter writer = new StreamWriter(logFileName, true))
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now.ToLongTimeString()+"  "+message);
                        //writer.WriteLine("\t\t"+message);
                        writer.Flush();
                        writer.Close();
                    }
                    catch (Exception exec)
                    {
                        PrintExceptionLog(exec);
                    }
                }
            }
            catch (Exception ex)
            {
                PrintExceptionLog(ex);
            }
        }
        public void WriteErrorLog(string err)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(_logDirectory);
                string logFileName = _logDirectory + "\\" +GetFileName()+"err.log";
                if (!dir.Exists) //不存Error文件夹则创建
                {
                    dir.Create();
                }
                using (StreamWriter writer = new StreamWriter(logFileName, true))
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + err);
                        //writer.WriteLine("\t\t"+message);
                        writer.Flush();
                        writer.Close();
                    }
                    catch (Exception exec)
                    {
                        PrintExceptionLog(exec);
                    }
                }
            }
            catch (Exception ex)
            {
                PrintExceptionLog(ex);
            }
        }
        public void WriteErrorLog(string fileName, string errInfo)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(_logDirectory);
                string logFileName = _logDirectory + "\\" + fileName+GetFileName() +"err.log";
                if (!dir.Exists) //不存Error文件夹则创建
                {
                    dir.Create();
                }
                using (StreamWriter writer = new StreamWriter(logFileName, true))
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + errInfo);
                        //writer.WriteLine("\t\t"+message);
                        writer.Flush();
                        writer.Close();
                    }
                    catch (Exception exec)
                    {
                        PrintExceptionLog(exec);
                    }
                }
            }
            catch (Exception ex)
            {
                PrintExceptionLog(ex);
            }
        }
        public void WriteException(string fileName, Exception ex)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(_logDirectory);
                string logFileName = _logDirectory + "\\" + fileName+GetFileName() + "ex.log";
                if (!dir.Exists) //不存Error文件夹则创建
                {
                    dir.Create();
                }
                using (StreamWriter writer = new StreamWriter(logFileName, true))
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + ex.Message);
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + ex.StackTrace);
                        writer.Flush();
                        writer.Close();
                    }
                    catch (Exception exec)
                    {
                        PrintExceptionLog(exec);
                    }
                }
            }
            catch (Exception exx)
            {
                PrintExceptionLog(exx);
            }
        }
        public void WriteException(Exception ex)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(_logDirectory);
                string logFileName = _logDirectory + "\\" +GetFileName()+ "ex.log";
                if (!dir.Exists) //不存Error文件夹则创建
                {
                    dir.Create();
                }
                using (StreamWriter writer = new StreamWriter(logFileName, true))
                {
                    try
                    {
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + ex.Message);
                        writer.WriteLine(DateTime.Now.ToLongTimeString() + "  " + ex.StackTrace);
                        writer.Flush();
                        writer.Close();
                    }
                    catch (Exception exec)
                    {
                        PrintExceptionLog(exec);
                    }
                }
            }
            catch (Exception exx)
            {
                PrintExceptionLog(exx);
            }
        }

        private void PrintLog(string errPos, string errMsg)
        {
            string logFileName = _logDirectory + "\\error1.log";
            StreamWriter sw = new StreamWriter(logFileName, true, System.Text.Encoding.Default);
            sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":" + errPos + "--" + errMsg);
            sw.Flush();
            sw.Close();
        }

        private void PrintExceptionLog(Exception ex)
        {
            PrintLog("---------------产生异常", "-----------------");
            PrintLog("Message --{0}", ex.Message);
            PrintLog("HelpLink --{0}", ex.HelpLink);
            PrintLog("Source --{0}", ex.Source);
            PrintLog("StackTrace --{0}", ex.StackTrace);
            PrintLog("TargetSite --{0}", ex.TargetSite.ToString());
            PrintLog("---------------异常结束", "-----------------");
        }
    }


}
